﻿
Imports std

Public MustInherit Class function_call_base(Of PARAMETERS_T As {iScript})
    Inherits sentence
    Implements iInvokeableWithOutput

    Protected Delegate Function createDelegate(ByVal name As String,
                                               ByVal iparameters As PARAMETERS_T) As function_call_base(Of PARAMETERS_T)

    Public Const parameterStart As Char = character.leftBracket
    Public Const parameterEnd As Char = character.rightBracket

    Protected Friend Shared Shadows Function _isMeaningfulMark(ByVal c As Char) As Boolean
        Return isParameterStart(c) OrElse isParameterEnd(c)
    End Function

    'for function_call4
    Protected Shared Function isParameterStart(ByVal c As Char) As Boolean
        Return c = parameterStart
    End Function

    Protected Shared Function isParameterEnd(ByVal c As Char) As Boolean
        Return c = parameterEnd
    End Function

    Private functionName As String = Nothing
    Private parameters As PARAMETERS_T = Nothing
    Private f As [function] = Nothing

    Protected Sub New(ByVal name As String, ByVal iparameters As PARAMETERS_T)
        functionName = name
        parameters = iparameters
    End Sub

    'for value
    Public Overloads Function invoke(ByVal ctx As context, _
                                     ByRef output As bare_variable) As Boolean Implements iInvokeableWithOutput.invoke
        Return invokebase(ctx, output)
    End Function

    Protected Overrides Function invokeImpl(ByVal ctx As context, ByRef output As bare_variable) As Boolean
        assert(Not ctx Is Nothing, "ctx is nothing.")
        If f Is Nothing Then
            Return ctx.invoke(functionName, parameters, output, Me, f)
        Else
            Return ctx.invoke(f, parameters, output, Me)
        End If
    End Function

    'for sentence, do not care about the return value
    Protected Overrides Function invokeImpl(ByVal ctx As context) As Boolean
        Return invokeImpl(ctx, convertor.cast(Of bare_variable)(Nothing))
    End Function

    Protected Shared Function _parse(ByVal input As String, ByRef index As Int64, _
                                     ByRef output As function_call_base(Of PARAMETERS_T), _
                                     ByVal parameter_t_parse As iParseable.parseImplDelegate(Of PARAMETERS_T), _
                                     ByVal create As createDelegate) _
                                    As Boolean
        assert(Not create Is Nothing)
        Dim name As String = Nothing
        If function_name_defination.parse(input, index, name) AndAlso _
           token(input, index, Nothing, AddressOf isParameterStart, AddressOf [false]) Then
            Dim pl As PARAMETERS_T = Nothing
            If parameter_t_parse(input, index, pl) AndAlso _
               token(input, index, Nothing, AddressOf isParameterEnd, AddressOf [false]) Then
                output = create(name, pl)
                Return True
            End If
        End If

        Return False
    End Function
End Class
